home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / SoundApps / Patchmix / Source / PatchWindow.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.6 KB  |  169 lines

  1. // PatchWindow.m
  2. // By Jayson Adams, NeXT Developer Support Team
  3. // You may freely copy, distribute and reuse the code in this example.
  4. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  5. // fitness for any particular use.
  6.  
  7. #import <stdlib.h>
  8. #import <objc/Storage.h>
  9.  
  10. #import "PatchView.h"
  11.  
  12. #import "PatchWindow.h"
  13.  
  14.  
  15. #define NOVIEWRECT -1
  16.  
  17. typedef struct _ViewRectPair {
  18.     id    view;
  19.     NXRect    rect;
  20.     } ViewRectPair;
  21.  
  22.  
  23. @implementation PatchWindow
  24.  
  25.  
  26. /* instance methods */
  27.  
  28. - initContent:(const NXRect *)contentRect style:(int)aStyle
  29.   backing:(int)bufferingType buttonMask:(int)mask defer:(BOOL)flag
  30. {
  31.     [super initContent:contentRect
  32.        style:aStyle
  33.        backing:bufferingType
  34.        buttonMask:mask
  35.        defer:flag];
  36.  
  37.     viewRectList = [[Storage alloc] initCount:0
  38.                     elementSize:sizeof(ViewRectPair)
  39.                     description:"{@ffff}"];
  40.     
  41.     viewRectUnderPoint = NOVIEWRECT;
  42.     
  43.     return self;
  44. }
  45.  
  46. - registerRect:(NXRect *)rect forView:view
  47. {
  48.     ViewRectPair    *newViewRect;
  49.     
  50.   /* add the view-rect pair to our list */
  51.     newViewRect = (ViewRectPair *)malloc(sizeof(ViewRectPair));
  52.     newViewRect->view = view;
  53.     newViewRect->rect = *rect;
  54.     
  55.     [viewRectList addElement:newViewRect];
  56.     
  57.     return self;
  58. }
  59.  
  60. - (BOOL)windowEntered:(NXPoint *)mouseLocation fromSource:dragSource
  61. {
  62.     NXPoint        windowPoint;
  63.     int            newViewRectUnderPoint;
  64.     ViewRectPair    *viewRect, *oldViewRect;
  65.     BOOL        oldViewDrew = NO, newViewDrew = NO;
  66.         
  67.   /* convert the mouse location to local coordinates */
  68.     windowPoint = *mouseLocation;
  69.     [self convertScreenToBase:&windowPoint];
  70.     
  71.   /* see if any of the views in our list lay under the mouse point */
  72.     newViewRectUnderPoint = [viewRectList count];
  73.     while (newViewRectUnderPoint--) {
  74.     viewRect = (ViewRectPair *)[viewRectList
  75.                               elementAt:newViewRectUnderPoint];
  76.     if (NXPointInRect(&windowPoint, &(viewRect->rect))) {
  77.         break;
  78.     }
  79.     }
  80.     
  81.   /* see if the mouse has moved over a different view */
  82.     if (viewRectUnderPoint != newViewRectUnderPoint) {
  83.       /* tell the view previously under the mouse that the window has exited */
  84.     if (viewRectUnderPoint != NOVIEWRECT) {
  85.         oldViewRect = (ViewRectPair *)[viewRectList
  86.                          elementAt:viewRectUnderPoint];
  87.         oldViewDrew = [oldViewRect->view windowExited:dragSource];
  88.     }
  89.       /* save the new view under the mouse */
  90.     viewRectUnderPoint = newViewRectUnderPoint;
  91.     
  92.       /*
  93.        * if there's a view under the mouse point, tell it that a window has
  94.        * entered it
  95.        */
  96.     if (viewRectUnderPoint != NOVIEWRECT) {
  97.         viewRect = (ViewRectPair *)[viewRectList
  98.                             elementAt:viewRectUnderPoint];
  99.         newViewDrew = [viewRect->view windowEntered:dragSource];
  100.     }
  101.     }
  102.     
  103.   /* let the caller know if any of our views has done any drawing */
  104.     return (oldViewDrew || newViewDrew);
  105. }
  106.  
  107. - (BOOL)windowExited:dragSource
  108. {
  109.     ViewRectPair    *viewRect;
  110.     BOOL        viewDrew = NO;
  111.     
  112.   /* tell any view previously under the mouse point that it no longer is */
  113.     if (viewRectUnderPoint != NOVIEWRECT) {
  114.     viewRect = [viewRectList elementAt:viewRectUnderPoint];
  115.     viewDrew = [viewRect->view windowExited:dragSource];
  116.     viewRectUnderPoint = NOVIEWRECT;
  117.     }
  118.     
  119.   /* tell the caller whether or not the view did some drawing */
  120.     return viewDrew;
  121. }
  122.  
  123. - (BOOL)windowDropped:(NXPoint *)mouseLocation fromSource:source
  124. {
  125.     NXPoint        windowPoint;
  126.     int            newViewRectUnderPoint;
  127.     ViewRectPair    *viewRect;
  128.     BOOL        accepted = NO;
  129.     
  130.   /* convert the mouse location to local coordinates */
  131.     windowPoint = *mouseLocation;
  132.     [self convertScreenToBase:&windowPoint];
  133.     
  134.   /* see if any of the views in our list lay under the mouse point */
  135.     newViewRectUnderPoint = [viewRectList count];
  136.     while (newViewRectUnderPoint--) {
  137.     viewRect = (ViewRectPair *)[viewRectList
  138.                              elementAt:newViewRectUnderPoint];
  139.     if (NXPointInRect(&windowPoint, &(viewRect->rect))) {
  140.         break;
  141.     }
  142.     }
  143.  
  144.   /* tell any view previously under the mouse point that the window moved */
  145.     if (viewRectUnderPoint != NOVIEWRECT &&
  146.     viewRectUnderPoint != newViewRectUnderPoint) {
  147.     
  148.     viewRect = (ViewRectPair *)[viewRectList elementAt:viewRectUnderPoint];
  149.     [viewRect->view windowExited:source];   
  150.     }
  151.     
  152.   /*
  153.    * tell the view under the mouse point (if any) that the user dropped a
  154.    * window on it
  155.    */
  156.     if (newViewRectUnderPoint != NOVIEWRECT) {
  157.     viewRect = (ViewRectPair *)[viewRectList
  158.                                elementAt:newViewRectUnderPoint];
  159.     accepted = [viewRect->view windowDropped:source:&windowPoint];
  160.     }
  161.     
  162.   /* no window under the mouse now */
  163.     viewRectUnderPoint = NOVIEWRECT;
  164.     
  165.     return accepted;
  166. }
  167.  
  168. @end
  169.